package com.damon.g2048.view;

import com.damon.g2048.R;
import com.damon.g2048.R.drawable;
import com.damon.g2048.R.id;
import com.damon.g2048.R.layout;
import com.damon.g2048.log.ELog;
import com.damon.g2048.log.LogFactory;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * 卡片view 2，4，8，16，32，64，128，256，512，1024，2048
 * 
 * @author damon.han
 * 
 */
public class Card extends FrameLayout {
	private int num;
	private TextView label;
	private View mainView;
	private ELog elog;

	public Card(Context context) {
		this(context, null);

	}

	public Card(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		elog = LogFactory.createLog();
		mainView = inflate(context, R.layout.card, this);
		label = (TextView) mainView.findViewById(R.id.card_txt_num);
		setNum(0);
	}

	public Card(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public int getNum() {
		return num;
	}

	/**
	 * 设值
	 * 
	 * @param num
	 */
	public void setNum(int num) {
		this.num = num;
		setBg(num);
		if (num <= 0) {
			label.setText("");
		} else {
			label.setText(num + "");
		}
	}

	/**
	 * 设置卡片背景色
	 */
	private void setBg(int num) {
		int bgId = 0;
		switch (num) {
		case 0:
			bgId = R.drawable.shape_card;
			break;
		case 2:
			bgId = R.drawable.shape_card;
			break;
		case 4:
			bgId = R.drawable.shape_card4;
			break;
		case 8:
			bgId = R.drawable.shape_card8;
			break;
		case 16:
			bgId = R.drawable.shape_card16;
			break;
		case 32:
			bgId = R.drawable.shape_card32;
			break;
		case 64:
			bgId = R.drawable.shape_card64;
			break;
		case 128:
			bgId = R.drawable.shape_card128;
			break;
		case 256:
			bgId = R.drawable.shape_card256;
			break;
		case 512:
			bgId = R.drawable.shape_card512;
			break;
		case 1024:
			bgId = R.drawable.shape_card1024;
			break;
		case 2048:
			bgId = R.drawable.shape_card2048;
			break;
		default:
			bgId = R.drawable.shape_card;
			break;
		}
		setBackgroundResource(bgId);
	}

	/**
	 * 判断两个卡片是不是相同
	 * 
	 * @param card
	 * @return
	 */
	public boolean equals(Card card) {
		if (card.getNum() == getNum()) {
			return true;
		} else {
			return false;
		}
	}

	public TextView getLabel() {
		return label;
	}
	
}
